package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
import org.light.utils.WriteableUtil;

public class CorsGenerator extends Util{
	public CorsGenerator(){
		super();
		super.fileName = "cors.go";
	}

	@Override
	public String generateUtilString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package middlewares"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import ("));
		sList.add(new Statement(4000L,1,"\"fmt\""));
		sList.add(new Statement(5000L,1,"\"net/http\""));
		sList.add(new Statement(6000L,0,""));
		sList.add(new Statement(7000L,1,"\"github.com/gin-gonic/gin\""));
		sList.add(new Statement(8000L,0,")"));
		sList.add(new Statement(9000L,0,""));
		sList.add(new Statement(10000L,0,"func Cors() gin.HandlerFunc {"));
		sList.add(new Statement(11000L,1,"return func(c *gin.Context) {"));
		sList.add(new Statement(12000L,2,"method := c.Request.Method"));
		sList.add(new Statement(13000L,2,"origin := c.Request.Header.Get(\"Origin\")"));
		sList.add(new Statement(14000L,2,"fmt.Println(\"JerryDebug:\", origin)"));
		sList.add(new Statement(15000L,2,"//if origin != \"\" {"));
		sList.add(new Statement(16000L,2,"c.Header(\"Access-Control-Allow-Origin\", \"*\") // 可将将 * 替换为指定的域名"));
		sList.add(new Statement(17000L,2,"c.Header(\"Access-Control-Allow-Headers\", \"*\")"));
		sList.add(new Statement(18000L,2,"c.Header(\"Access-Control-Allow-Methods\", \"*\")"));
		sList.add(new Statement(19000L,2,"c.Header(\"Access-Control-Expose-Headers\", \"*\")"));
		sList.add(new Statement(20000L,2,"c.Header(\"Access-Control-Allow-Credentials\", \"true\")"));
		sList.add(new Statement(21000L,2,"//}"));
		sList.add(new Statement(22000L,2,"if method == \"OPTIONS\" {"));
		sList.add(new Statement(23000L,3,"c.AbortWithStatus(http.StatusNoContent)"));
		sList.add(new Statement(24000L,2,"}"));
		sList.add(new Statement(25000L,2,"c.Next()"));
		sList.add(new Statement(26000L,1,"}"));
		sList.add(new Statement(27000L,0,"}"));

		return WriteableUtil.merge(sList).getContent();
	}

}
